package com.mycar2w.cloud.service.repository;

import com.mycar2w.cloud.entity.TTransaction;
import com.mycar2w.cloud.service.constant.ConstantInfo;
import com.umuw.cloud.core.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

/**
 * created time 2017/4/7
 *
 * @author ljc
 * @version V1.0
 */
@Repository
public interface TTransactionRepository extends BaseRepository<TTransaction>{

    default Page<TTransaction> findTransactions(Long userId, Integer pageIndex){
        Pageable pageAble=new PageRequest(pageIndex, ConstantInfo.PAGE_SIZE,new Sort(Sort.Direction.DESC,"id"));
        Specification<TTransaction> spec=(root,query,cb)->{
            Path<Long> buyUserId=root.get("buyUserId");
            Path<Long> saleUserId=root.get("saleUserId");
            Predicate pre=cb.or(buyUserId.in(userId),saleUserId.in(userId));
            return pre;
        };
        return this.findAll(spec,pageAble);
    }

    TTransaction findById(Long transactionId);
}
